package com.atguigu.dao.impl;

import com.atguigu.pojo.Order;
import com.atguigu.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @Author luoyin
 * @Date 12:47 2022/7/23
 **/
public class OrderDaoImpl1{
    public void add(Order order) throws Exception {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try{
            connection =  JDBCUtils.getConnection();
            String sql = "insert into t_order values (null,?,?,?,?,?,?)";
            statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            statement.setString(1, order.getOrderSequence());
            statement.setString(2, order.getCreateTime());
            statement.setInt(3, order.getTotalCount());
            statement.setDouble(4, order.getTotalAmount());
            statement.setInt(5, order.getOrderStatus());
            statement.setInt(6, order.getUserId());
            int update = statement.executeUpdate();
            if (update > 0) {
                resultSet = statement.getGeneratedKeys();
                if (resultSet.next()) {
                    int orderId = resultSet.getInt(1);
                    order.setOrderId(orderId);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.release(connection,statement,resultSet);
        }

    }
}
